package com.ssy.lingxi.search.controller.mobile;

import com.ssy.lingxi.common.constant.tree.Node;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.search.api.model.request.CommoditySearchRequest;
import com.ssy.lingxi.search.api.model.request.ShopSearchRequest;
import com.ssy.lingxi.search.api.model.response.CommoditySearchResponse;
import com.ssy.lingxi.search.service.ICommodityService;
import com.ssy.lingxi.search.service.IScoreService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @menu: APP-积分兑换-企业商城和店铺商城
 * 积分兑换-企业商城和店铺商城
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/8/1
 */
@RestController
@RequestMapping("/search/mobile/shop/score")
public class MobileScoreController extends BaseController {

    @Resource
    private ICommodityService commodityService;

    @Resource
    private IScoreService scoreService;

    /**
     * 查询商品品类树
     * @param storeId 店铺id
     * @return
     */
    @RequestMapping(value = "/getCategoryTree", method = RequestMethod.GET)
    public Wrapper<List<Node>> getCategoryTree(@RequestParam(value = "storeId", required = false) Long storeId) {
        Long shopId = this.getHeadersShopId();
        return Wrapper.success(scoreService.getCategoryTree(shopId, storeId));
    }

    /**
     * 查询商品列表
     * @param shopSearchRequest 查询条件实体
     * @return
     */
    @RequestMapping(value = "/getCommodityList", method = RequestMethod.POST)
    public Wrapper<PageData<CommoditySearchResponse>> searchCommodityList(@RequestBody ShopSearchRequest shopSearchRequest) {
        Long shopId = this.getHeadersShopId();
        CommoditySearchRequest commoditySearchRequest = this.modelMapper.map(shopSearchRequest, CommoditySearchRequest.class);
        commoditySearchRequest.setShopId(shopId);
        //判断是否已登录
        if(this.isLogin()){
            UserLoginCacheDTO sysUser = this.getSysUser();
            commoditySearchRequest.setLoginMemberId(sysUser.getMemberId());
            commoditySearchRequest.setLoginMemberRoleId(sysUser.getMemberRoleId());
        }
        return Wrapper.success(commodityService.searchCommodityList(commoditySearchRequest, true, true));
    }

}
